<template>
    <div class="indexContainer">
        <div class="navtop">
            <div>
                <el-button
                    v-for="(item, index) in navList"
                    :key="index"
                    @click="showLeftnav(item)"
                    :type="getType()"
                    round
                >{{item.forkName}}</el-button>
            </div>
            <div>
                <el-button type="success" @click="getCassoData">备忘录</el-button>
                <el-button @click="goCreate">写文章&备忘录</el-button>
            </div>
        </div>
        <div class="content">
            <div class="contentright">
                <Detailarticel
                    :flag="isRemark"
                    :htmlval="htmlval"
                    :titleval="selectTitle"
                    :forkname="forknames"
                    :selectData="selectData"
                ></Detailarticel>
            </div>
        </div>
        <el-drawer size="20%" :title="navLeftTitle" :visible.sync="drawer" :direction="'rtl'">
            <div class="drawerContainer">
                <div
                    @click="showDetails(item)"
                    v-for="(item, index) in letNavList"
                    :key="index"
                    class="selectListItem"
                >
                    <div class="title">{{item.title}}</div>
                    <div class="time">{{item.add_time | timeFormat}}</div>
                </div>
            </div>
        </el-drawer>
    </div>
</template>

<script>
// import Detailarticel from "./details.vue";
const Detailarticel = () => import("./details.vue");
export default {
    data() {
        return {
            navList: [],
            letNavList: [],
            flag: true,
            drawer: false,
            htmlval: "",
            navLeftTitle: "",
            selectTitle: "",
            forknames: "",
            isRemark: false,
            selectData: ""
        };
    },
    components: {
        Detailarticel
    },
    methods: {
        // 点击导航按钮显示右边目录列表
        showLeftnav(item) {
            this.selectTitle = "";
            this.isRemark = false;
            this.$ajax
                .get("/article/?fork=" + item.pk)
                .then(res => {
                    if (res.data.results.length > 0) {
                        this.letNavList = res.data.results;
                        this.navLeftTitle = item.forkName;
                        this.drawer = true;
                    } else {
                        this.$notify({
                            message: "暂无数据!",
                            type: "warning",
                            position: "bottom-left"
                        });
                        this.letNavList = [];
                        this.navLeftTitle = item.forkName;
                    }
                })
                .catch(err => {
                    this.$notify({
                        message: "网络错误!",
                        type: "warning"
                    });
                    this.letNavList = [];
                    this.navLeftTitle = item.forkName;
                });
        },
        // 改变导航按钮的type
        getType() {
            let num = Math.random();
            let str = "";
            if (0 < num < 0.3) {
                str = "danger";
            } else if (0.3 < num < 0.5) {
                str = "warning";
            } else if (0.5 < num < 0.7) {
                str = "info";
            } else if (0.7 < num < 1) {
                str = "success";
            } else {
                str = "danger";
            }
            return str;
        },
        // getNav 获取导航按钮
        getNav() {
            this.$ajax
                .get("/forks")
                .then(res => {
                    //this.navList = res.data.results;
                    if (res.data.results.length > 0) {
                        this.navList = res.data.results;
                        this.$store.commit("pushForkList", res.data.results);
                    } else {
                        this.$notify({
                            message: "暂无数据!",
                            type: "warning",
                            position: "bottom-left"
                        });
                        this.letNavList = [];
                    }
                })
                .catch(err => {
                    this.$notify({
                        message: "网络错误!",
                        type: "warning"
                    });
                });
        },
        // 点击查看文章
        showDetails(item) {
            this.selectTitle = "";
            // console.log(item);
            this.selectData = item;
            this.htmlval = item.content;
            this.selectTitle = item.title;
            if (this.isRemark) {
            } else {
                this.forknames = item.forkby.forkName;
            }
        },
        goCreate() {
            this.$router.push({ name: "create" });
        },
        // 备忘录
        getCassoData() {
            this.isRemark = true;
            this.selectTitle = "";
            let token = window.localStorage.getItem("token");
            if (token == "" || token == null) {
                this.$router.push({ name: "login" });
            } else {
                this.$ajax
                    .get("/remarks")
                    .then(res => {
                        // console.log(res.data);
                        if (res.data.results.length > 0) {
                            this.letNavList = res.data.results;
                            this.drawer = true;
                        } else {
                            this.$notify({
                                message: "暂无数据!",
                                type: "warning"
                            });
                            this.letNavList = [];
                        }
                    })
                    .catch(err => {
                        this.$notify({
                            message: "获取备注时网络错误!",
                            type: "warning"
                        });
                        this.letNavList = [];
                    });
            }
        }
    },
    mounted() {
        this.getNav();
    }
};
</script>

<style lang="scss" scoped>
.indexContainer {
    width: 100%;
    .navtop {
        height: 45px;
        width: 100%;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
        position: absolute;
        padding-top: 6px;
        top: 0;
        text-align: center;
        display: flex;
        justify-content: space-around;
    }
    .content {
        padding-top: 50px;
        width: 100%;
        display: flex;
        justify-content: space-between;
        .contentright {
            width: 100%;
            height: auto;
        }
    }
    .selectListItem {
        padding: 15px;
        display: flex;
        margin-top: 5px;

        cursor: pointer;
        justify-content: space-between;
        .title {
            font-size: 14px;
            font-family: "微软雅黑";
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            opacity: 0.8;
        }
        .time {
            font-size: 14px;
            font-family: "微软雅黑";
            opacity: 0.5;
        }
    }
    .selectListItem:hover {
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
        background-color: #67c23a;
    }
    .drawerContainer {
        height: 95%;
        width: 100%;
        overflow: scroll;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
        position: absolute;
        top: 50px;
        left: 0;
    }
}
</style>